import { $get } from '@js/authRequest'
import { awaitWrap } from '@js/tool'

// 获取统计数据信息
export async function getData (condition, order, orderField, gfield, fieldall) {
  const res = await $get('/api/xapc/casestat/statByCaseFieldTable', {
    condition: JSON.stringify(condition),
    order_field: orderField,
    order,
    gfield,
    fieldall
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取全部统计数据
export async function getTotal (condition) {
  const res = await $get('/api/xapc/casestat/statSumData', {
    condition: JSON.stringify(condition)
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取案件类型树
export async function getCaseTypeTree () {
  const res = await $get('/api/xapc/dictionary/getTreeByType', {
    type: '案件分类'
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 格式化案件类型树对象
export async function getFinanceCauseAction () {
  const [e, d] = await awaitWrap(getCaseTypeTree())
  if (e) {
    throw e
  }
  let list = []
  for (let i = 0; i < d.length; i++) {
    if (d[i].value === '金融案件') {
      list = d[i].children.map(li => li.value)
      break
    }
  }
  return list
}
